package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ROAD_MAP, desc = "押镖路线")
public class BeautyEscortRoadMapCmd extends AbstractCommand {

    int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortRoadMapReqMsg reqMsg = BeautyEscortProto.BeautyEscortRoadMapReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(type != 0 && type != 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        boolean groupOpen = player.getModule(BeautyEscortModule.class).systemOpen();
        if(groupOpen){
            if(type == 1){
                //刷新消耗  预扣除
                Property consume = PropertyHelper.parseStringToProperty(GameConfig.ESCORT_REFRESH_CAR_CONSUME);
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
                player.getModule(PreReduceModule.class).preReduce(PropertyHelper.parsePropertyToString(consume));
            }
            BeautyEscortProto.CrossBeautyEscortRoadMapReqMsg.Builder reqMsg = BeautyEscortProto.CrossBeautyEscortRoadMapReqMsg.newBuilder();
            UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
            if(unionInfo != null) {
                reqMsg.setUnionLv(UnionConfigMgr.getUnionLv(unionInfo.getExperience()));
            }else {
                reqMsg.setUnionLv(0);
            }
            long totalGlamour = BeautyEscortMgr.getBeautyTotalGlamour(player);
            reqMsg.setTotalGlamour(totalGlamour);
            reqMsg.setType(type);
            reqMsg.setUnionUid(player.getUserInfo().getUnionUid());
            player.sendPacket(Protocol.C_BEAUTY_ESCORT_ROAD_MAP, reqMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortRoadMapRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortRoadMapRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROAD_MAP, respMsg);
        }
    }
}
